package datastructure.tree;

/**
 *
 * @author Lei
 */
public class GetNodeCount {

    public static int getNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        else {
            return 1 + getNodeCount(root.left) + getNodeCount(root.right);
        }
    }

    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(4);
        TreeNode n5 = new TreeNode(5);
        TreeNode n6 = new TreeNode(6);

        n5.left = n2;
        n5.right = n6;

        n2.left = n1;
        n2.right = n4;

        n4.left = n3;

        System.out.println(GetNodeCount.getNodeCount(n5));

    }
}
